from playwright.sync_api import Page, expect, Playwright
from Config.config import *
from Config.URL import *
import allure, re
import json
import string

#获取指定接口
def urlget(response):
    if "viewPorcess/getViewRecord" in response.url:
        seturl(response,response.url)
        print("获取指定接口:"+response.url)




def test_case(playwright : Playwright) -> None:
    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page
    page.goto(url+url_login)
    #page.locator("#drag1").drag_to(page.locator("#div2"))
    #tool.element_drag_and_drop(page,page.locator("#drag1"),{'x':100,'y':100})

    page.get_by_placeholder("请输入用户名").fill(username_admin)
    page.get_by_placeholder("请输入密码").fill(password_admin)
    page.get_by_role("button", name="登 录").click()
    
    page.goto(url + url_jianlidashiji)
    page.wait_for_timeout(2000)
    title = "AutoTest通用文档|2024-03-28 13:56:25"
    page.locator("form").filter(has_text="标题查询清空").get_by_placeholder("请输入标题").fill(title)
    page.get_by_role("button", name=" 查询").click()
    page.wait_for_timeout(500)
    page.get_by_role("cell", name=title).click()
    page.get_by_role("button", name="取 消").click()
    page.get_by_role("button", name=" 版本更新").click()
    page.wait_for_timeout(1000)

    # page.get_by_text("未填报").first.click()
    # page.get_by_role("button", name=" 表单填报").click()
    # page.wait_for_timeout(1000)
    # page.get_by_role("button", name="保存", exact=True).click()
    # expect(page.locator("body")).to_contain_text("填报成功")
    # #page.get_by_role("cell", name="CB01-").locator("div").click()
    # page.get_by_text("未填报").first.click()
    # page.get_by_role("button", name=" 表单填报").click()
    # page.get_by_label("表单填报").locator("i").nth(2).click()
    # with page.expect_file_chooser() as fc_info:
    #     page.locator("div").filter(has_text=re.compile(r"^上传填报后文件$")).nth(2).click()
    # file_chooser = fc_info.value
    # file_chooser.set_files(test_datas_dir + "\填报.docx")
    # expect(page.locator("body")).to_contain_text("导入成功")
    #page.get_by_role("button", name="保存草稿").click()


    # allure.step("登录")
    # page.goto(url)
    # page.get_by_placeholder("请输入用户名").fill('shigong1')
    # page.get_by_placeholder("请输入密码").fill(password_admin)
    # page.get_by_role("button", name="登 录").click()
    # 
    # allure.step("跳转CB01")
    #
    # page.goto(url + url_CB01)
    # page.get_by_role("button", name=" 查询").click()
    # page.get_by_text("AutoTestCB01在线填报").first.click()
    # with page.expect_popup() as page1_info:
    #     page.get_by_text("签章文件").first.click()
    # page1 = page1_info.value
    #
    # # 测试结果截图
    # page.wait_for_timeout(1000)
    # filename = os.path.join(test_screenshot_dir, f"{'签章文件1'}.png")
    # page1.screenshot(path=filename)
    # allure.attach.file(source=filename, name='签章文件1',
    #                    attachment_type=allure.attachment_type.PNG)
    #http://form.power-itech.cn:18004/#/projectLevel/constructionSupervision/constructionLog/constructionLog

#     page.goto("http://172.18.3.26:7300/web/#/projectLevel/sonGatewayHome/index")
#     page.get_by_placeholder("请输入用户名").fill(username_admin)
#     page.get_by_placeholder("请输入密码").fill(password_admin)
#     page.get_by_role("button", name="登 录").click()
#     page.wait_for_timeout(2000)
#     #存储cookie
#     context.storage_state(path="cookie.json")
#
#     # 1.点击发起流程数据
#     page.get_by_label("待办事宜").get_by_text("【施工安全日志】 2024-03-09施工安全日志").click()
#     # 2.监控流程接口，获取流程接口
#     # 监听请求事件
#     #page.on("request", lambda request: print("request: ", request.method, request.url))
#     # 监听响应事件
#     page.on("response", urlget)
#     page.wait_for_timeout(2000)
#     # 3.解析接口获取审批人
#     # 读取cookie JSON文件
#     with open('cookie.json', 'r', encoding='utf-8') as f:
#         data = json.load(f)
#         print(data)
#     # 提取cookie数据--给接口使用（这里只是打印出来）
#     print(data["origins"][0]["localStorage"][5]["value"])
#     # cookie后缀带|String--删除掉
#     webcookie = data["origins"][0]["localStorage"][5]["value"].replace("|String", "")
#     head = {
#         # "Accept": "application/json, text/plain, */*",
#         # Add GitHub personal access token.
#         "Authorization": webcookie,
#     }
#     #context = playwright.request.new_context(base_url='http://172.18.3.26:7300')
#     httpcontext = playwright.request.new_context()
#     response = httpcontext.get(url = geturl(page),headers=head)
#     responsejson = response.json()
#     print("解析接口获取审批人:")
#     print(responsejson)
#     people = responsejson["data"]["runTask"][0]["handledDtoList"][0]["handledByName"]
#     print("下节点审批人："+people)
#     # 4.审批人转换为账号
#     approvalpeople = 'admin'
#     if people == "谭监":
#         approvalpeople = ("jianli1")
#     if people == "陈凯":
#         approvalpeople = ("shigong1")
#     if people == "管理员":
#         approvalpeople = ("admin")
#     # 5.登录审批人账号，进行审批（通过cookies登录）
#     context.close()
#     context = browser.new_context(storage_state="cookie.json")
#     page = context.new_page()
#     page.goto('http://172.18.3.26:7300/web/#/projectLevel/sonGatewayHome/index')
#     
#
#     page.get_by_label("待办事宜").get_by_text("【施工安全日志】 2024-03-09施工安全日志").click()
#     if page.get_by_placeholder("请输入签署密码").is_visible():
#         page.get_by_placeholder("请输入签署密码").fill(password_flow)
#     if page.get_by_placeholder("请选择签章").is_visible():
#         page.get_by_placeholder("请选择签章").click()
#         page.get_by_placeholder("请选择签章").press("ArrowDown")
#         page.get_by_placeholder("请选择签章").press("Enter")
#         #page.get_by_text("监理单位项目1部-企业公章").click()
#     page.get_by_role("button", name="通 过").click()
#     expect(page.locator("body")).to_contain_text("操作成功")
#     # 6.解析接口获取审批人
#     # 7.审批人转换为账号
#     # 8.登录审批人账号，进行审批（通过cookies登录）
#     # 9.解析接口获取不到审批人，结束
# #http://172.18.3.26:7300/api/bpm/viewPorcess/getViewRecord/da084ef5-dc61-11ee-ad97-3ecd11eeef14?n=1710828676
# #流程编号 : 2400003088
# #http://172.18.3.26:7300/api/bpm/viewPorcess/getViewRecord/3d1cee4b-d550-11ee-94f7-1a726b201e27?n=1710828948
# #流程编号 : 2400002858
# #http://172.18.3.26:7300/api/bpm/viewPorcess/getViewRecord/9e1da7f9-e2ac-11ee-aefe-3ecd11eeef14?n=1710829060
# #流程编号 : 2400003149
# # 1.点击发起流程数据
# # 2.监控流程接口
# # 3.解析接口获取审批人
# # 4.审批人转换为账号
# # 5.登录审批人账号，进行审批（通过cookies登录）
# # 6.解析接口获取审批人
# # 7.审批人转换为账号
# # 8.登录审批人账号，进行审批（通过cookies登录）
# # 9.解析接口获取不到审批人，结束
